package club.gskj.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author : Connor-G 康纳-郭
 * @date : 14:51 21.1.3
 * @Company : http://www.zzrgsoft.org
 * @Version : 1.0
 */

/**
 * 使用网关解决 前台访问跨域问题.
 */
@Configuration
public class GuliMallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //  1.配置跨域
        //  允许请求头
        corsConfiguration.addAllowedHeader("*");
        //  允许请求方法
        corsConfiguration.addAllowedMethod("*");
        //  允许请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 允许卸载cookie跨域
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
